<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="X-UA-Compatible" content="IE=8">
<meta http-equiv="Expires" content="0">
<meta http-equiv="Pragma" content="no-cache">
<meta http-equiv="Cache-control" content="no-cache">
<meta http-equiv="Cache" content="no-cache">
    <meta name="viewport" content="width=device-width,minimum-scale=1.0,maximum-scale=1.0,user-scalable=no"/>
    <title>灵犀支付</title>
    <script src="javascript/jquery.min.js"></script>
    <script src="javascript/function.js"></script>
    <link rel="stylesheet" href="stylesheet/style_v1.1.css">
         <script src="http://res.wx.qq.com/open/js/jweixin-1.0.0.js"></script>
</head>
<body>
<div class="header-title">
    <a href="javascript:void(0);" class="fanhui">
        <img src="image/tick.png" alt="">
    </a>
    <p>灵犀支付</p>
</div>
<div class="con padding-top60 padding-bottom50 padding-lr5">
    <div class="xingxiang">
        <h4>选择印象</h4>
        <div id="lableList">
        </div>
    </div>
      <div class="liuyan">
        <h4>选择留言</h4>
        <div class="l1" id="messageShow">
<!--             <span> <img src="image/t2.png" alt=""> </span> -->
        </div>
    </div>
<!--       <input type="text" class="inputs" placeholder="请输入留言" id="messgae"> -->
<!--     <select class="selects" id="messageList"> -->
<!--         <option id="" class="0">--请选择留言--</option> -->
<!--         <option value="从小玩到大">从小玩到大</option> -->
<!--     </select> -->
<!--     <select class="selects"> -->
<!--         <option>赞助数量</option> -->
<!--         <option value="1">1</option> -->
<!--         <option value="2">2</option> -->
<!--         <option value="3">3</option> -->
<!--         <option value="4">4</option> -->
<!--         <option value="5">5</option> -->
<!--     </select> -->
    <p class="zz-jine">合计：赞助 <span>1</span> 次，金额：<span id="price">0</span> 元</p>
<!--       <p class="zz-jine">合计：赞助 <span>10</span>次，金额：<span>99.00</span>元</p> -->
    <a href="javascript:void(0);" class="btn" onclick="cliffordPay();">灵犀一下(支付)</a>
<!--     <a href="javascript:void(0);" class="btn" onclick="cliffordPay();">确认</a> -->
</div>
	<%@ include file="footer.jsp"%>
<script>
var selectTag;
var messageId;

var cliffordId='<%=request.getParameter("parms.cliffordId")%>';
    $(function () {
    	queryClifford();
    	queryLable();
//     	queryMessage();
    });
    function queryMessageList(tagId) {
		var params = "parms.tagId=" + tagId;
	$.ajax({
				type : 'post',
				url : 'cliffordAction!queryMessage.action',
				async : false,
					data : params, // 传给action的参数
				success : function(data) {
					messageId="";
					if (data.result == 1) {
						if (data.resultData.size == '0') {
							document.getElementById("messageShow").innerHTML="";
							alert("该印象没有留言信息");
						} else {
							var html = "";
							var list = data.resultData.datalist;
							for ( var i = 0; i < list.length; i++) {
								html += '   <span id='+list[i].blessId+'> <img src="'+list[i].blessImg+'" alt=""> </span>';
							}
							document.getElementById("messageShow").innerHTML = html;
					    	 $('.liuyan').find('span').on('click', function () {
					             $('.liuyan').find('span').removeClass('a-xuanzhong');
					             $(this).addClass('a-xuanzhong');
					             
					             messageId=$(this).attr("id");
					         });
						}
					} else {
						document.getElementById("messageShow").innerHTML="";
						alert(data.msg);
					}
				}
			});
}
    function queryMessage(tagId) {
		var params = "parms.tagId=" + tagId;
	$.ajax({
				type : 'post',
				url : 'cliffordAction!queryMessage.action',
				async : false,
					data : params, // 传给action的参数
				success : function(data) {
					var html = "";
					html += ' <option id="" class="0">--请选择留言--</option>';
					if (data.result == 1) {
						if (data.resultData.size == '0') {
							document.getElementById("messageList").innerHTML=html;
							alert("该印象没有留言信息");
						} else {
							var list = data.resultData.datalist;
							for ( var i = 0; i < list.length; i++) {
								html += ' <option id="'+list[i].blessId+'" class="'+list[i].blessId+'">'+list[i].blessName+'</option>';
							}
							document.getElementById("messageList").innerHTML = html;
						}
					} else {
						document.getElementById("messageList").innerHTML=html;
						alert(data.msg);
					}
				}
			});
}
    
    function queryLable() {
// 		var params = "parms.goodsId=" + goodsId;
	$.ajax({
				type : 'post',
				url : 'cliffordAction!queryLable.action',
				async : false,
//					data : params, // 传给action的参数
				success : function(data) {
					selectTag="";
					if (data.result == 1) {
						if (data.resultData.size == '0') {
							document.getElementById("lableList").innerHTML="";
							alert("未获取到印象列表");
						} else {
							var list = data.resultData.datalist;
							var html = "";
							for ( var i = 0; i < list.length; i++) {
								html += '  <span id="'+list[i].labelId+'">';
									html += '   <img src="'+list[i].labelImg+'" alt="">';
					         	html += '  </span>';
// 								html += ' <span id="'+list[i].labelId+'">'+list[i].labelName+'</span>';
							}
							document.getElementById("lableList").innerHTML = html;
							$('.xingxiang').find('span').on('click',function(){
					            $('.xingxiang').find('span').removeClass('a-xuanzhong');
					            $(this).addClass('a-xuanzhong');
					            selectTag=$(this).attr("id");
// 					            queryMessage(selectTag);
					            queryMessageList(selectTag);
					        });
						}
					} else {
						document.getElementById("lableList").innerHTML="";
						alert(data.msg);
					}
				}
			});
}
    function queryClifford() {
   		var params = "parms.cliffordId=" + cliffordId;
    	$.ajax({
    				type : 'post',
    				url : 'cliffordAction!queryCliffordDetailByCliffordId.action',
    				async : false,
   					data : params, // 传给action的参数
    				success : function(data) {
    					if (data.result == 1) {
    							var list = data.resultData.datalist;
        						$("#price").text(list.cliffordPrice);
    					} else {
    						alert(data.msg);
    					}
    				}
    			});
    }
    function cliffordPay(){
		var money="0.01";
//    		var money=$("#price").text();
   		if(money=="0"){
   			alert("未获取到正确的灵犀价格，请刷新页面");
   			return;
   		}
   		if(selectTag==""){
   			alert("请选择印象");
   			return;
   		}
   		if(messageId==""){
   			alert("请选择留言");
   			return;
   		}
    	if (isWeiXin()) {
//     		  var selectItem=$("#messageList").find("option:selected");
    		
    		var params="parms.cliffordId="+cliffordId+"&parms.price="+money;
    		params+="&parms.tag="+selectTag;
    		params+="&parms.message="+messageId;
//     		params+="&parms.message="+$("#messgae").val();
//     		 if(typeof(selectItem.attr('class')) != "undefined"&&selectItem.attr('class')!="0"){
// 				 params += "&parms.message=" + selectItem.attr('class');
// 			 }else{
// 				 alert("请选择留言");
// 				 return;
// 			 }
		 	$.ajax({
		 		type : 'post',
		 		url : 'weixinjson!makeWeixinClifford.action',
		 		data : params, // 传给action的参数
		 		async:false,
		 		success : function(data) { 
		 			if(data.msg=="true"){
		 				if (typeof WeixinJSBridge == "undefined"){
		 				   if( document.addEventListener ){
		 				       document.addEventListener('WeixinJSBridgeReady', onBridgeReady, false);
		 				   }else if (document.attachEvent){
		 				       document.attachEvent('WeixinJSBridgeReady', onBridgeReady); 
		 				       document.attachEvent('onWeixinJSBridgeReady', onBridgeReady);
		 				   }
		 				}else{
		 				   onBridgeReady(data);
		 				}
		 			}else{
		 				alert(data.msg);
		 			}
		 		}
		 	});
    	}else{
    		 alert("请在微信中打开!");
    	}
    }
	  function onBridgeReady(data){
		   WeixinJSBridge.invoke(
		       'getBrandWCPayRequest', {
		           "appId":data.resultData.config.weixin_appId,     //公众号名称，由商户传入     
		           "timeStamp":data.resultData.config.timestamp,         //时间戳，自1970年以来的秒数     
		           "nonceStr" : data.resultData.config.nonceStr, //随机串     
		           "package": data.resultData.config.packageInfo,     
		           "signType" : "MD5",         //微信签名方式：     
		           "paySign" : data.resultData.config.signature //微信签名 
		       },
		       function(res){     
		           if(res.err_msg == "get_brand_wcpay_request:ok" ) {
	 	        	   alert("支付成功");
	 	        	  location.href=document.referrer;
		           } else{
		        	   alert("支付失败");
//	 	        	   alert(res.err_code+":"+res.err_desc+":"+res.err_msg +"支付失败");
		           }
		           
		       }
		   );  
	}
</script>
</body>
</html>